package Servlet;

import DAO.DAOFactory;
import DAO.MaintenanceDAO;
import javabean.Customer;
import javabean.Maintenance;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/MyMaintenance")
public class MyMaintenanceServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MaintenanceDAO maintenanceDAO = (MaintenanceDAO) DAOFactory.getMaintenanceDAOInstance();
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("loginCustomer");
        List<Maintenance> maintenanceList = null;
        List<Maintenance> UnassignedMaintenanceList = null;
        try {
            //获取未分配的维修单
            UnassignedMaintenanceList = maintenanceDAO.addUnassignedMaintenanceByCID(customer.getCustomerID());
            //获取已分配的维修单
            maintenanceList = maintenanceDAO.getMaintenanceByCID(customer.getCustomerID());
            request.setAttribute("UnassignedMaintenanceList", UnassignedMaintenanceList);
            request.setAttribute("maintenanceList", maintenanceList);
            request.getRequestDispatcher("MyMaintenance.jsp").forward(request, response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
